var app = getApp();
const cnConvert = require('../../utils/changefont.js');
Page({

    data: {
        user:{},
        location:'',
        noBind:false,
        banner:[],
        notice:[],
        offset:0,
        dataList:[],
        loading:true,
        isFrist:true,
        noData:false,
        isDaili:0,
        home:'首页',
        hdata:'数据',

        pageText:{
          'title'   : '咖缘吧运营管理系统',
          'position': '定位中',
          'location': '当前位置',
          'corder'  : '咖啡订单量',
          'gorder'  : '商品定单量',
          'cprice'  : '咖啡销售额',
          'gprice'  : '商品销售额',
          'totle'   : '合计',        
        }
    },
   onLoad: function (options) {   
    this.getLocation();
    this.getBanner();
    this.getNotice(0);
    var that  = this;
    this.data.user = wx.getStorageSync('userDo');
    if(this.data.user.font == 1){
      this.getNotice(1);
    }
    if (!this.data.user){
    
      app.getUserInfo().then(res => {
        if(res){
          this.setData({
            user: res,
            noBind: false,
          })
          wx.showTabBar({
            animation: true
          })
          this.getDataList(true);
          this.setData({
            isFrist: false
          })
         
        }else{
          this.setData({
            noBind: true,
            loading: false
          })
          wx.hideTabBar({
              animation: true
          })
        }
      })
    }else{
      this.getDataList(true);
      this.setData({
        isFrist:false
      })
    }
          
   },
  
   onShow:function(){  
       var that =this;  
       if (wx.getStorageSync('userDo')) {
           this.getDataList(true)
           this.setData({
               noBind: false,
           })      
          
           wx.showTabBar({
             animation:true
           })
           if (wx.getStorageSync('userDo').font == 1) { //切换繁体 
            
             that.getNotice(1); 
             var textObj = this.data.pageText
             for (var item in textObj) {
               textObj[item] = cnConvert.traditionalized(textObj[item])
             }
             this.setData({
               pageText: textObj,
               home: '首頁',
               hdata:'數據'
               
             })
             wx.setNavigationBarTitle({ title: this.data.pageText.title })
           }
          
       }else{
         this.setData({
           noBind: true
         })
         wx.hideTabBar({
           animation: true
         })
       }

       wx.setTabBarItem({
         'index': 0,
         text: this.data.home
       })
       wx.setTabBarItem({
         'index': 1,
         text: this.data.hdata
       })


   },
   onHide:function(){
       this.setData({
           isFrist: false
       })
   },
  

   goBind:function(e){
     var islogin = e.target.dataset.login || ''

      wx.setStorageSync('prevPage', 'home')
       wx.navigateTo({
         url: '/pages/bindAccount/bindAccount?login='+islogin,
       })
   },
   detail:function(e){
        wx.navigateTo({
            url: '/pages/orderDetails/orderDetails?days='+e.currentTarget.dataset.days,
        })
   },
    getBanner:function(){
        var that = this
        wx.request({
            url: app.globalData.api + 'getPagePic',
            success:function(res){
                that.setData({
                    banner:res.data
                })
            }
        })
    },
    lower:function(){
        var page = this.data.offset
        page++
        this.setData({
            offset: page
        })
        this.getDataList();
    },
    getDataList:function(init){
        var user = wx.getStorageSync('userDo');
        var that = this;
        that.setData({
            loading:true
        })
        if(init){
          that.setData({
            offset:0
          })
        }
        wx.request({
            url: app.globalData.api + 'coffeeOrderCountList',
            data:{
                makerUserId: user.userId,
                userTypee: user.type,
                offset: that.data.offset,
                type:1,
                leagueId:that.data.isDaili || ''

            },
            success: function (res) {
                 if (res.statusCode == 200) {
                   if (res.data.length){
                   
                    var list = []
                    if(init){
                      list = res.data
                      
                    }else{
                      list = that.data.dataList
                      for (var i = 0; i < res.data.length; i++) {
                        list.push(res.data[i])
                      }
                    }
                    
                    that.setData({
                      dataList: list,
                       loading: false
                    })
                   }else{
                     that.setData({
                       noData: true
                     })
                   } 
                 }
            }
        })
    },

    getNotice:function(font){
        var that = this
        wx.request({
          url: app.globalData.api + 'announcementList/5/' + font,
            success: function (res) {
                if(res.data.length == 1){
                    var list = res.data
                    list.push(res.data[0]);
                    that.setData({
                        notice: list
                    })
                }else{
                    that.setData({
                        notice: res.data
                    })
                }             
            }
        })
    },
    getLocation:function(){
      var that = this;
      wx.getLocation({
          type: 'gcj02',
          success: function (res) {
              let latitude = res.latitude;
              let longitude = res.longitude;      
              wx.request({
                  url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=HN3BZ-DRA6J-EZ7FW-K2OXV-ADMMF-G2BH7',
                  method: "get",
                  success: function (res) {
                    var l = res.data.result.address_reference.crossroad.title
                    if (wx.getStorageSync('userDo').font == 1){
                      l = cnConvert.traditionalized(l)
                    }
                    that.setData({
                      location:l
                    })
                  }
              })
          },
         
      })            
     }


})